package com.iseastar.guojiang.newcabinet;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.iseastar.guojiang.app.Intents;
import com.shouhuobao.station.R;
import com.zbar.camera.CameraManager;
import com.zbar.client.CaptureActivity;

import droid.frame.activity.title.TitleMgr;
import droid.frame.activity.title.TitleRes;

/**
 * Created by joker on 2017/12/22.
 * 任务扫描二维码
 */
public class TaskCaptureActivity extends CaptureActivity {

    /**
     * 当前闪光灯是否打开
     */
    private boolean isOpenFlash;

    @Override
    public void onCreate(Bundle icicle) {
        String action = getIntent().getAction();
        String title = "";
        if (Intents.courier_scan_for_task.equals(action)) {
            setLayout(R.layout.capture_task, true);
            title = "扫码";

        } else if (Intents.courier_scan_for_tansfer.equals(action)) {
            setLayout(R.layout.capture_task, true);
            title = "扫描火箭侠二维码";
        }
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(icicle);
        TitleMgr titleMgr = new TitleMgr(this);
        titleMgr.initTitle();
        final ImageView titleRightImage = (ImageView) findViewById(R.id.title_right_image);
        titleMgr.setTitle(new TitleRes[]{titleMgr.getBackTitle(), new TitleRes(title), new TitleRes(R.drawable.title_flashlight_on_selector, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isOpenFlash) {// 当前是打开的
                    // cameraManager.setTorch(false);// 关闭闪光灯
                    CameraManager.get().offLight();
                    isOpenFlash = false;
                    titleRightImage.setImageResource(R.drawable.title_flashlight_on_selector);
                } else {// 当前闪光灯是关闭的
                    // cameraManager.setTorch(true);// 打开
                    CameraManager.get().openLight();
                    isOpenFlash = true;
                    titleRightImage.setImageResource(R.drawable.title_flashlight_off_selector);
                }
            }
        })});
        //火箭侠任务中扫描二维码
        if (Intents.courier_scan_for_task.equals(action)) {
            ((TextView) findViewById(R.id.scan_tip_tv)).setText("请扫描用户二维码");
        }
        //火箭侠移动仓管理中心扫描二维码
        else if (Intents.courier_scan_for_tansfer.equals(action)) {
            ((TextView) findViewById(R.id.scan_tip_tv)).setText("扫描火箭侠二维码完成交接");
        }
    }

    @Override
    protected boolean callback(String contents) {
        // 驿站扫码
        Intent data = new Intent();
        data.putExtra("result", contents);
        setResult(RESULT_OK, data);
        finish();
        return true;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 100 && resultCode == RESULT_OK && data != null) {
            setResult(RESULT_OK, data);
            finish();
        }
    }
}
